#!/bin/sh

#-------------------------------------#
#  SGT (Software Generation Toolkit)  #
#-------------------------------------#
#                                     #
#      Find the specified file        #
#      in all dependencies            #  
#                                     #
#  GDE 03/07/2003 v3.2                #
#       check \MAK subdirectory       #
#                                     #
#  GDE 22/04/2003 v3.1                #
#       Correct some errors           #
#                                     #
#  GDE 20/02/2003 v3.0                #
#       Modify the name from          #
#       find_lib to find_file         #
#                                     #
#  GDE 06/12/2002 v2.0                #
#       Modify the list of arguments  #
#                                     #
#  GDE 15/10/2002 v1.1                #
#       update for SGT 1.1.5          #
#                                     #
#  POU 09/10/2002 v1.0                #
#       creation                      #
#                                     #
#-------------------------------------#
# $1: specific option                 #
#-------------------------------------#



#-------------------------------------#
# Script informations
#-------------------------------------#
export SCRIPT_SGT_VER=3.2
export SCRIPT_SGT_DATE=03/07/2003
export SCRIPT_SGT_USER=GDE
export SCRIPT_SGT_NAME=find_file
export SCRIPT_SGT_USAGE="Find the specified file in all dependencies"
#-------------------------------------#



#---------------------
# Create and go to output directory
#---------------------
if [ -d mak ]
then
	mkdir -p -m775 out
	cd out
else
	export MSG_ERROR_SCRIPT=0
	export CMD_SCRIPT=Error
fi
#---------------------



#-------------------------------------#
# Parameters section
#-------------------------------------#
#-------------------------------------#
# Default parameters
#-------------------------------------#
export COMPILE_LIB=off
export USE_DEP=off


#---------------------
# Parameter 2 -> specific option
#---------------------
if [ ! -z $2 ] && [ -z $CMD_SCRIPT ]
then
	if [ $2 = -c ]
	then
		export COMPILE_LIB=on
		export USE_DEP=off
	else
		if [ $2 = -d ]
		then
			export COMPILE_LIB=on
			export USE_DEP=on
		else
			export MSG_ERROR_SCRIPT=1
			export CMD_SCRIPT=Error
		fi
	fi
fi


#---------------------
# Parameter 1 -> target file / specific option
#---------------------
if [ ! -z $1 ] && [ -z $CMD_SCRIPT ]
then
	if [ $1 = -v ]
	then
		export CMD_SCRIPT=Version
	else
		if [ $1 = -h ]
		then
			export CMD_SCRIPT=Help
		else
			if [ $1 = --help ]
			then
				export CMD_SCRIPT=Help
			else
				if [ $1 = -hm ]
				then
					export CMD_SCRIPT=More
				else
					if [ $1 = -u ]
					then
						export CMD_SCRIPT=Usage
					fi
				fi
			fi
		fi
	fi
fi


#-------------------------------------#
# if no option is defined, display help
#-------------------------------------#
if [ $# -eq 0 ] && [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Help
fi


#-------------------------------------#
# Process section
#-------------------------------------#
#---------------------
# Analyse all dependency files
#---------------------
if [ -z $CMD_SCRIPT ]
then
	echo " "
	echo "    $SCRIPT_SGT_USAGE"
	echo " "
	#---------------------
	# List all *.dep files in current directory
	#---------------------
	FileFound=0;
	list=`find -iname '*.dep' -type f`
	for i in $list
	do
		# Find the first parameter in current file
		filetype=`grep $1 $i`
		if [ -n "$filetype" ]
		then
			argument=`basename $i .dep`;
			if [ $COMPILE_LIB = on ]
			then
				cd ..;
				if [ $USE_DEP = on ]
				then
					make_lib $argument -d;
				else
					make_lib $argument;
				fi
				cd out;
			else
				echo  "        $argument";
			fi
			FileFound=1;
		fi
	done
	if [ $FileFound = 0 ]; then
		echo $1" isn't in .dep files !!";
		echo "";
	fi
	exit
fi



#-------------------------------------#
# Help script section
#-------------------------------------#
if [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Version
fi


#---------------------
# Usage
#---------------------
if [ $CMD_SCRIPT = Usage ]
then
	echo "  $SCRIPT_SGT_NAME	$SCRIPT_SGT_USAGE"
	exit
fi

#---------------------
# Version
#---------------------
echo " "
echo "-------------------------------------"
echo " '$SCRIPT_SGT_NAME' (release "$SCRIPT_SGT_VER") for SGT "$SGT_VER". Last update by "$SCRIPT_SGT_USER" on "$SCRIPT_SGT_DATE
echo "  $SCRIPT_SGT_USAGE"
if [ $CMD_SCRIPT = Version ]
then
	exit
fi

#---------------------
# Simple help
#---------------------
if [ $CMD_SCRIPT = Help ]
then
	echo "-------------------------------------"
	echo " $SCRIPT_SGT_NAME [FILE_TO_FIND] [-OPTION]"
	echo " "
	echo "  -c    Update the library linked to FILE_TO_FIND by compiling it"
	echo "  -d    Idem that -c but enable dependencies for the current library"
	echo " "
	echo "  -h | --help  Display this help "
	echo "  -hm          Display more help about SGT" $SGT_VER
	echo "  -v           Display the version of the current script"
fi

#---------------------
# More help
#---------------------
if [ $CMD_SCRIPT = More ]
then
	make -f ../mak/gen.mak help USE_TARGET_FILE=OFF
fi



#-------------------------------------#
# Error messages section
#-------------------------------------#

if [ $CMD_SCRIPT = Error ]
then
	if [ $MSG_ERROR_SCRIPT = 0 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : The root path is not a valid SGT directory. Please check for the \MAK sub-directory"
	fi
	if [ $MSG_ERROR_SCRIPT = 1 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : unknown option. Check syntax use !"
		echo " "
	fi
	echo " "
	echo " Check your command line or see help section -> $SCRIPT_SGT_NAME -h"
fi
